package com.zmn.plat.admin.controller.servcategory;

import javax.servlet.http.HttpServletRequest;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.common.exception.PlatException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.StatusConsts;
import com.zmn.plat.business.interfaces.servcategory.ServAssCategoryBService;

/**
 * @author linfeng
 * @version v1.0
 * @since 2019/09/23 9:15
 * @类说明：前台服务分类、前台产品分类关联功能
 */
@Controller
@RequestMapping("/serv/showCateg/ass/")
public class ShowServAssCategoryModifyController extends ServCategoryBaseController {
    private Logger logger = LoggerFactory.getLogger(ShowServAssCategoryModifyController.class);

    @Autowired
    private ServAssCategoryBService servAssCategoryBService;

    @PostMapping(value = "batchAddAssociation")
    @ResponseBody
    public ResponseDTO batchAddAssociation(HttpServletRequest request, @RequestBody JSONObject object) {
        if (null == object) {
            return ResponseDTO.fail(StatusConsts.STATUS_ERROR, "请检查参数是否正确！");
        }

        try {
            servAssCategoryBService.batchAddAssociation(getMcStaffRealName(), object);
            return ResponseDTO.success(true,"操作成功");
        }catch (PlatException e){
            return ResponseDTO.fail(e.getMessage());
        }catch (Exception e){
            logger.error("batchAddAssociation error",e);
            return ResponseDTO.fail("操作失败");
        }

    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public ResponseDTO delete(@RequestParam(value = "ids", defaultValue = "0") String groupIds) {
        if (StringUtils.isEmpty(groupIds)) {
            return ResponseDTO.fail(StatusConsts.STATUS_ERROR,  "请选择要删除的数据！");
        }

        // servAssCategoryService.deleteByKeys(groupIds.split(","));
        showCategoryAssociationService.deleteByServCategIdAndCategOneIdAndCategId(groupIds.split(","));
        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
    }

    @RequestMapping(value = "updateDesc")
    @ResponseBody
    public ResponseDTO updateDesc(@RequestParam(value = "id") Integer id, @RequestParam(value = "desc") String desc) {
        if (id == null) {
            return ResponseDTO.fail(StatusConsts.STATUS_ERROR,  "请选择要修改的的数据！");
        }

        servAssCategoryService.updateDesc(id, desc);

        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS,  "操作成功！");
    }
}
